using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using Models.APIModels;
using Models.ServerModels;

namespace DdnsServer.DnspodAPI
{
    public class DomainApi :APIBase
    {
        public DomainApi(ApiConfigModel cfg):base(cfg)
        {

        }
        #region 添加新域名
        /// <summary>
        /// 添加域名
        /// </summary>
        /// <param name="domain">域名, 没有 www, 如 dnspod.com</param>
        /// <param name="group_id">域名分组ID, 可选参数</param>
        /// <param name="is_mark">是否星标域名，”yes”、”no” 分别代表是和否</param>
        /// <returns></returns>
        public AddDomainRetMsgModel Create(string domain,string group_id="",string is_mark="no")
        {
            Dictionary<string, object> dc = new Dictionary<string, object>();
            dc["domain"] = domain;
            if (!string.IsNullOrEmpty(group_id))
                dc["group_id"] = group_id;
            if (!string.IsNullOrEmpty(is_mark))
                dc["is_mark"] = is_mark;
            return PostData<AddDomainRetMsgModel>("Domain.Create", dc);
        }
        #endregion
        #region 获取域名列表
        /// <summary>
        /// type 域名分组类型, 默认为’all’. 包含以下类型：
        /// --all：所有域名
        /// --mine：我的域名
        /// --share：共享给我的域名
        /// --ismark：星标域名
        /// --pause：暂停域名
        /// --vip：VIP域名
        /// --recent：最近操作过的域名
        /// --share_out：我共享出去的域名
        /// offset  记录开始的偏移, 第一条记录为 0, 依次类推
        /// length    要获取的域名数量, 比如获取20个, 则为20
        /// group_id  分组ID, 获取指定分组的域名。可以通过 获取域名分组 获取 group_id
        /// keyword    搜索的关键字, 如果指定则只返回符合该关键字的域名
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public DomainListModel GetDomainList(string keyword="",string groupid="",string type="all",int offset=0,int length=3000)
        {
            Dictionary<string, object> dc = new Dictionary<string, object>();
            if (!string.IsNullOrEmpty(keyword)) dc["keyword"] = keyword;
            if (!string.IsNullOrEmpty(groupid)) dc["group_id"] = groupid;
            dc["type"] = type;
            dc["offset"] = offset;
            dc["length"] = length;
            return PostData<DomainListModel>("Domain.List", dc);
        }
        #endregion
        #region 删除域名
        public RetMessageModel DelDomain(int domain_id=-1,string domain="")
        {
            if(domain_id==-1 && domain=="")
                throw new Exception("必须指定要删除的域名");
            Dictionary<string, object> dc = new Dictionary<string, object>();
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<RetMessageModel>("Domain.Remove");
        }
        #endregion
        #region 设置域名状态
        /// <summary>
        /// 设置域名状态，域名与域名ID提交一个即可
        /// </summary>
        /// <param name="status">状态enable和disable</param>
        /// <param name="domain_id">域名ID</param>
        /// <param name="domain">域名</param>
        /// <returns></returns>
        public RetMessageModel SetDomainStatus(string status, int domain_id=-1,string domain="")
        {
            if(domain_id ==-1 && domain=="")
                throw new Exception("必须指定要设置的域名");
            Dictionary<string, object> dc = new Dictionary<string, object>();
            if(status !="enable" && status !="disable")
                throw new Exception("状态不正确");
            dc["status"] = status;
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<RetMessageModel>("Domain.Status", dc);
        }
        #endregion
        #region 获取域名信息
        public DomainInfoModel GetDomainInfo(int domain_id=-1,string domain="")
        {
            if(domain_id==-1 && domain=="")
                throw new Exception("必须指定要查询的域名");
            Dictionary<string, object> dc = new Dictionary<string, object>();
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<DomainInfoModel>("Domain.Info", dc);
        }
        #endregion
        #region 获取域名日志
        public DomainLogModel GetDomainLog(int domain_id=-1,string domain="",int offset=0,int length=500)
        {
            if(domain_id==-1 && domain=="")
                throw new Exception("必须指定要查询的域名");
            Dictionary<string, object> dc = new Dictionary<string, object>
            {
                ["offset"] = offset,
                ["length"] = length
            };
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<DomainLogModel>("Domain.Log", dc);
        }
        #endregion
        #region 设置搜索引擎推送
        /// <summary>
        /// 设置搜索引擎推送
        /// </summary>
        /// <param name="status">推送开关 yes/no</param>
        /// <param name="domain_id">域名id</param>
        /// <param name="domain">域名</param>
        /// <returns></returns>
        public RetMessageModel SetSearchEnginePush(string status,int domain_id=-1,string domain="")
        {
            if(domain_id==-1&&domain=="")
                throw new Exception("必须指定要修改的域名");
            Dictionary<string, object> dc = new Dictionary<string, object>();
            dc["status"] = status;
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<RetMessageModel>("Domain.Searchenginepush", dc);
        }
        #endregion
        #region 添加域名共享
        public RetMessageModel AddShareDomain(string email, int domain_id = -1, string domain = "", string mode = "r", string sub_domain = "")
        {
            if(string.IsNullOrEmpty(email))
                throw new Exception("邮箱必须指定");
            if(domain_id==-1&&domain=="")
                throw new Exception("必须指定要共享的域名");
            Dictionary<string, object> dc = new Dictionary<string, object>();
            dc["email"] = email;
            dc["mode"] = mode;
            if (!string.IsNullOrWhiteSpace(sub_domain))
                dc["sub_domain"] = sub_domain;
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<RetMessageModel>("Domainshare.Create", dc);
        }
        #endregion
        #region 域名共享列表
        public DomainShareListModel GetDomainShareList(int domain_id=-1,string domain="")
        {
            if(domain_id==-1 && domain=="")
                throw new Exception("必须指定要查询的域名");
            Dictionary<string, object> dc = new Dictionary<string, object>();
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            return PostData<DomainShareListModel>("DomainShare.List", dc);
        }
        #endregion
        #region 修改域名共享
        public RetMessageModel EditDomainShare(string email,int domain_id=-1,string domain="",string mode="r",string sub_domain="",string old_sub_domain="",string new_sub_domain="")
        {
            if(domain_id==-1&&domain=="")
                throw new Exception("必须指定要修改的域名");
            Dictionary<string, object> dc = new Dictionary<string, object>();
            dc["mode"] = mode;
            if (domain_id != -1)
                dc["domain_id"] = domain_id;
            else
                dc["domain"] = domain;
            if (sub_domain != "")
                dc["sub_domain"] = sub_domain;
            if (old_sub_domain != "")
                dc["old_sub_domain"] = old_sub_domain;
            if (new_sub_domain != "")
                dc["new_sub_domain"] = new_sub_domain;
            return PostData<RetMessageModel>("Domainshare.Modify", dc);
        }
        #endregion
        #region 删除共享域名
        public RetMessageModel DelDomainShare(string email,int domain_id=-1,string domain="")
        {
            try
            {
                if(string .IsNullOrWhiteSpace(email))
                    throw new Exception("必须指定邮箱");
                if(domain_id==-1&&domain=="")
                    throw new Exception("必须指定域名");
                Dictionary<string, object> dc = new Dictionary<string, object> { ["email"] = email };
                if (domain_id != -1)
                    dc["domain_id"] = domain_id;
                else
                    dc["domain"] = domain;
                return PostData<RetMessageModel>("Domainshare.Remove", dc);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
    }
}
